import java.util.HashMap;

public class twoSum {
    public int[] twoSum(int[] nums, int target) {
        //两数之和，我选择用hash表
        int[] res = new int[2];
        HashMap<Integer, Integer> map = new HashMap<>();
        for(int i = 0; i < nums.length; i++){
            if(map.containsKey(target - nums[i])){
                res[0] = map.get(target - nums[i]);
                res[1] = i;
                return res;
            }else{
                map.put(nums[i], i);
            }
        }
        return res;
    }
}
/**
 * 主测试类 - 可直接运行验证
 */
class Main {
    public static void main(String[] args) {
         twoSum solution = new twoSum();
         int[] nums = {2, 7, 11, 15};
         int target = 9;
         int[] res = solution.twoSum(nums, target);
         System.out.println("结果：" + res[0] + " " + res[1]);
     }
    }